package com.wang.bean;

import lombok.Getter;

import java.util.Collections;
import java.util.List;

/**
 * @Author wangf
 * @Description TODO
 * @Date 2023/9/3 21:26
 */
@Getter
public class PageResponse<T> {
    private Long current = 1L;
    private Long size = 10L;

    private Integer total = 0;
    private Integer totalPages = 0;

    private List<T> result = Collections.emptyList();

    private Integer start = 1;

    private Integer end = 0;

    public PageResponse() {
    }

    public void setTotalPages(Integer totalPages) {
        if (totalPages != null && totalPages > 0) {
            this.totalPages = totalPages;
        }
    }

    public void setCurrent(Integer current) {
        if (current != null && current > 0) {
            this.current = Long.valueOf(current);
        }
    }
    public void setPageSize(Integer pageSize) {
        if (pageSize == null || pageSize > 0) {
            this.size = Long.valueOf(pageSize);
        }
    }
    public void setTotal(Integer total) {
        this.total = total;
        if (total == -1) {
            this.current = 1L;
        }
        if (total != null && total > 0) {
            this.totalPages = Math.toIntExact(total % this.size == 0 ? total / this.size : total / this.size + 1);
        } else {
            this.totalPages = 0;
        }
        this.start = Math.toIntExact(this.current > 0 ? (this.current - 1) * this.size + 1 : 1);
        this.end = Math.toIntExact(this.start - 1 + this.size * (this.current > 0 ? 1 : 0));
    }

    public void setResult(List<T> result) {
        this.result = result;
        if (result != null && result.size() > 0 && this.total == 0) {
            setTotal(result.size());
        }
    }
}
